package com.yswy.backstage.controller;


import com.yswy.backstage.entity.user.User;
import com.yswy.backstage.param.UserPARAM;
import com.yswy.backstage.service.SsoService;
import com.yswy.common.model.ResultModel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/sso")
@ApiModel("登录相关")
public class SsoController {

    @Autowired
    private SsoService ssoService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "登录")
    public ResultModel login(@RequestBody @Validated(UserPARAM.Login.class) UserPARAM userPARAM) {
        return ssoService.login(userPARAM);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "注销")
    public ResultModel logout(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        return ssoService.logout(user);
    }

    /**
     * 获取验证码的通用接口
     *
     * @param reqBody
     * @return
     */
    /*@RequestMapping(value = "/requestValidateCode", method = RequestMethod.POST)
    public ResultModel requestValidateCode(@RequestBody String reqBody) {
        log.info("获取验证码: " + reqBody);

        ResponseVo responseVo = new ResponseVo();
        JSONObject req = new JSONObject(reqBody);
        String phoneNumber = req.getString("phoneNumber");
        MobileAuthCodeType verifyCodeType = MobileAuthCodeType.valueOf(req.getInt("verifyCodeType"));//0为注册，1为忘记复位，2为绑定微信
        if (!NumberValidaterTools.isMobileNumber(phoneNumber)) {
            responseVo.setCode(BaseErrorCode.General_Error);
            responseVo.setMessage("错误的手机号");
        } else {
            //if (userService.sendSMSVerifyCode(phoneNumber, verifyCodeType)) {
            Mobileauthcode mobileauthcode = userService.sendSMSVerifyCode(phoneNumber, verifyCodeType);
            if (mobileauthcode != null) {
                responseVo.setCode(BaseErrorCode.General_Success);
                responseVo.setMessage("发送验证码到手机，成功！");
                responseVo.setData(mobileauthcode.getAuthCode());
            } else {
                responseVo.setCode(SsoErrorCode.SendAuthCodeToPhoneFailed);
                responseVo.setMessage("发送验证码到手机，失败！");
            }
        }
        return responseVo;
    }*/


}



